﻿using Koala.Pro.CodeGenerator.UI.Common.Adapter;
using Koala.Pro.CodeGenerator.UI.DatabaseModel.Schema;
using Koala.Pro.CodeGenerator.UI.Models;

namespace Koala.Pro.CodeGenerator.UI.Common
{
    public class DatabaseSqlManager
    {
        public const string ProjectPath = "TestSqlScript";

        public static List<FileNameAndCode> GetCreateTableSql(string dbType, string tableName, ICollection<ColumnInfo> columnList)
        {
            List<FileNameAndCode> list = new List<FileNameAndCode>();
            if (dbType == "sqlite")
            {
                var sqlite = (DatabaseModel.Data.Sqlite)Program.global.DbInstance.DatabaseAssembly;
                var code = sqlite.GetCreateTableSQL(tableName);
                FileNameAndCode fileNameAndCode = new FileNameAndCode();
                fileNameAndCode.Code = code;
                list.Add(fileNameAndCode);
            }
            else
            {
                var adapter = new CreateTableAdapter(tableName, columnList, string.Format("{0}/Templates/{1}/CreateTable.sql.vm", "TestSqlScript", dbType));
                list.AddRange(adapter.ExecuteString());
            }

            return list;
        }

        public static List<FileNameAndCode> GetCommonSql(string dbType, string tableName, string sqlType, ICollection<ColumnInfo> columnList)
        {
            List<FileNameAndCode> list = new List<FileNameAndCode>();
            var adapter = ((sqlType.IndexOf("select", StringComparison.OrdinalIgnoreCase) >= 0) ? new CreateTableAdapter(tableName, columnList, string.Format("{0}/Templates/{1}/Select.sql.vm", "TestSqlScript", dbType)) : ((sqlType.IndexOf("update", StringComparison.OrdinalIgnoreCase) >= 0) ? new CreateTableAdapter(tableName, columnList, string.Format("{0}/Templates/{1}/Update.sql.vm", "TestSqlScript", dbType)) : ((sqlType.IndexOf("delete", StringComparison.OrdinalIgnoreCase) < 0) ? new CreateTableAdapter(tableName, columnList, string.Format("{0}/Templates/{1}/Insert.sql.vm", "TestSqlScript", dbType)) : new CreateTableAdapter(tableName, columnList, string.Format("{0}/Templates/{1}/Delete.sql.vm", "TestSqlScript", dbType)))));
            list.AddRange(adapter.ExecuteString());
            return list;
        }
    }

}
